

var datas=""
var mainCustomerId=sessionStorage.getItem("mainCustomerId")//主客户id
var mainCustomerName=sessionStorage.getItem("mainCustomerName")//主客户名称
maincustomer()
function maincustomer() {
    $("#mainCustomerName").html(mainCustomerName)
    $.ajax({
        url:root.member.getCustomers,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":50,
            "customerCode":$("#customerCode").val(),
            "customerId":$("#customerId").val(),
            "customerName":$("#customerName").val(),
            "mainCustomerId":mainCustomerId,
            "isOnlyFreeOfMain":"",
            "belongGroupId":""
        }),
        success: function (data) {
            datas=data.data.data
            couponlist(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        /* root.member.adminMember2CList*/
                        url: root.member.getCustomers,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":50,
                            "customerCode":$("#customerCode").val(),
                            "customerId":$("#customerId").val(),
                            "customerName":$("#customerName").val(),
                            "mainCustomerId":mainCustomerId,
                            "isOnlyFreeOfMain":"",
                            "belongGroupId":""

                        }),
                        success: function (result) {
                            couponlist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
var table;   //table对象
dataSource = [];
function couponlist(data, index){
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data.data));
    dataSource.splice(0,dataSource.length);//清空数组
    if(data.data !=null && data.data != ''){
        var datashu =data.startRow+1;
        $.each(data.data, function (i, field) {
            var datashus = datashu++
            var rowData = new Array();
            rowData.push(field.customerCode);
            rowData.push(field.customerId);
            rowData.push(field.customerName);
            rowData.push(field.belongGroupName);
            rowData.push(datashus);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);
    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val='<span><a onclick="removes(\'' + row[1] + '\')">移除</a></span>';

                        return val;
                    },
                    "targets":5
                }


            ]
        });
    };
}


//确定和返回按钮
function maincustomerfh(val) {
    if(val==1){
        if(datas==""||datas==null){
            var curInd = layer.confirm(
                '<span>确定不添加客户吗？</span>'+'<br/>', {title:'提示',btn: ['确定', '取消']}, function(index){
                    var indexs = parent.location.reload();
                    parent.layer.close(indexs);
                    layer.close(curInd);

                },function(){
                    layer.close(curInd);

                });
        }else {
            var indexs = parent.location.reload();
            parent.layer.close(indexs);
        }

    }else {
        var indexs = parent.location.reload();
        parent.layer.close(indexs);
    }

}

//跳转页面
function customerjump(title, url) {
    sessionStorage.setItem("mainCustomerIds",mainCustomerId)
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

//移除
function removes(id) {
    var curInd = layer.confirm(
        '<span>移除后账号体系将解除关联，确定移除吗？</span>'+'<br/>', {title:'提示',btn: ['确定', '取消']}, function(index){
            $.ajax({
                url:root.member.relationMainCustomer,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                contentType: "application/json",
                data: JSON.stringify({
                    "manCustomerId":mainCustomerId,
                    "customerIds":id,
                    "removeRelation":"true",
                }),
                success: function (data) {
                    if(data.errorCode==0){
                        $.Huimodalalert("移除成功",2000);
                        maincustomer()
                    }else {
                        $.Huimodalalert(data.msg,2000);
                    }
                },
                error: function () {
                    $.Huimodalalert('失败！',2000);
                }
            });
            layer.close(curInd);

        },function(){
            layer.close(curInd);

        });

}
//主客户限制字符
var len
var Preser
var common=100//要限制输入的字数
function checkMaxIut(me) {
    var text=$(me).val()
    strlen(text)
    if(len>common){
        $(me).val(Preser)
    }else {
        Preser=$(me).val()
    }
}
function strlen(str){
    len = 0;
    for (var i=0; i<str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            len++;
        } else {
            len+=2;
        }
    }
    return len;
}